#include "aerRenderTexture.hpp"
#include "aerTexture.hpp"

namespace aer {


// RenderTexture ------------------------------
    
bool RenderTexture::setup(const aer::Texture *pOutput)
{ 
  m_pOutput = pOutput;  
  glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 
                          m_pOutput->getId(), 0);
  return CheckStatus();
}

void RenderTexture::begin()
{
  //glViewport( 0, 0, m_pOutput->getWidth(), m_pOutput->getHeight());  
  glDisable(GL_DEPTH_TEST);
  glDepthMask( GL_FALSE );
  
  bind();
  glDrawBuffer( GL_COLOR_ATTACHMENT0 );
}

void RenderTexture::end()
{  
  unbind();
  
  glEnable( GL_DEPTH_TEST );//
  glDepthMask( GL_TRUE );//
}


// RenderTextureDB ------------------------------    
    
bool RenderTextureDB::setup(const aer::Texture *pTex1, const aer::Texture *pTex2)
{
  m_pOutput[0] = pTex1;  
  glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 
                          m_pOutput[0]->getId(), 0);
                          
  m_pOutput[1] = pTex2;  
  glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, 
                          m_pOutput[1]->getId(), 0);
    
  return CheckStatus();
}

void RenderTextureDB::begin()
{
  bind();
  glDrawBuffer((m_out) ? GL_COLOR_ATTACHMENT1 : GL_COLOR_ATTACHMENT0 );
}

void RenderTextureDB::end()
{
  unbind();
  
  if (m_bAutoSwap) {
    swap();
  }
}


} // aer
